package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
	
/**
 * 安阳新闻网
 * @author Persh
 *
 */
public class AynewsNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.aynews.net.cn/Article/[0-9]{4}/[0-9]{6}/[0-9]*.html",
		"http://www.aynews.net.cn/[a-z]*/[0-9]*-[0-9]*/[0-9]*/.*.shtml"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("title").text();
		title = StringUtils.substringBefore(title, "-");
		content=doc.select("div#cnt").text();
		if(content.isEmpty()){
			content=doc.select("#cms_content_div").text();
		}

		author=JSoupUtils.matchAuthor(doc, "来源：","文章来源：");

//		date=DateUtils.matchDate(doc.select("div.info").text());
		date=JSoupUtils.matchDate(doc, "更新时间：","发布时间");
		

		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	public static void main(String[] args) {
		AynewsNewsAnalyse analyse=new AynewsNewsAnalyse();
		String url="http://www.aynews.net.cn/news/2016-11/04/cms235698article.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
